:-module(g8,[]).
:-use_module('de.pro').
:-use_module('gua.pro').
:-use_module('wx.pro').
:-use_module('dz.pro').

de:g8_items([坤,艮,坎,巽,震,离,兑,乾]).

new(Var,V):-
	de:new(Var,[count-8,value-V,type-g8,level-3]).

yang(G,T):-
	new(G,_),
	gua:yang(G,A),
	T is A /\ 1.

wuxing(G,W):-
	new(G,A),
	wx:new(W,B),
	(
	 A < 2 -> B = 4;
	 A > 5 -> B = 0;
	 A = 2 -> B = 1;
	 A = 5 -> B = 3;
	 B = 2
	).

luoshu(Gua,Order):-
	new(Gua,A),
	nth0(A,[2,8,1,4,3,9,7,6],Order).

najia(Index,Info):-
	(is_dict(Index,de) -> I = Index.value; I = Index),
	List = [
		_{up:9,down:1,zhi:7},
		_{up:2,down:2,zhi:4},
		_{up:4,down:4,zhi:2},
		_{up:7,down:7,zhi:1},
		_{up:6,down:6,zhi:0},
		_{up:5,down:5,zhi:3},
		_{up:3,down:3,zhi:5},
		_{up:8,down:0,zhi:0}
	       ],
	nth0(I,List,Info).

najia(Gua,Gong,Loc,Result):-
	new(Gua,GV),
	wuxing(Gong,Wx1), 
	najia(GV,Info),
	tg:new(Gan,Info.Loc),
	yang(Gua,Yang),
	(Yang = 1 -> Diff = 2; Diff = -2),
	T1 = _{up:3,down:0}, 
	findall(A,(
		   between(0,2,B),
		   C is B + T1.Loc,
		   D is (Info.zhi + C * Diff) mod 12,
		   dz:new(Zhi,D),
		   dz:wuxing(Zhi,Wx2),
		   Who is (Wx2.value - Wx1.value) mod 5,
		   A = [Gan,Zhi,Who]
		   ),Result).